﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ERP.BusinessEntities;
using Core;
using System.Threading;

namespace MainApp
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
            //this.size
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {
            panel1.BackColor = Color.FromArgb(41, 57, 85);

            UserNameTextBox.GotFocus += new EventHandler(UserNameTextBox_GotFocus);
            UserNameTextBox.LostFocus += new EventHandler(UserNameTextBox_LostFocus);

            PassWordTextBox.GotFocus += new EventHandler(UserNameTextBox_GotFocus);
            PassWordTextBox.LostFocus += new EventHandler(UserNameTextBox_LostFocus);
        }

        void UserNameTextBox_LostFocus(object sender, EventArgs e)
        {
            ((Control)sender).BackColor = Color.White;
        }

        void UserNameTextBox_GotFocus(object sender, EventArgs e)
        {
            ((Control)sender).BackColor = Color.FromArgb(255, 248, 225);
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void Login_Click(object sender, EventArgs e)
        {
            String strUser = UserNameTextBox.Text.Trim();
            String strPassword = PassWordTextBox.Text.Trim();

            if (String.IsNullOrEmpty(strUser))
            {
                ActiveControl = UserNameTextBox;
                return;
            }

            if (String.IsNullOrEmpty(strPassword))
            {
                ActiveControl = PassWordTextBox;
                return;
            }

            BSUsersInfo user = new BSUsersController().GetUser(strUser, strPassword);
            if (user == null)
            {
                MessageBox.Show("The user name or Password incorect, pleae try again");
                return;
            }

            AppContext.CurrentUserInfo = user;
            this.Close();
            try
            {
                Thread t = new Thread(StartMyForm);
                t.TrySetApartmentState(ApartmentState.STA);
                t.Start();
                
            }
            catch (Exception ex)
            {
            }
            
            //Application.Run(new MainForm());
        }
        public static void  StartMyForm()
        {
            Application.Run(new MainForm());
        }

        private void Control_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                if (String.IsNullOrEmpty(UserNameTextBox.Text))
                {
                    ActiveControl = UserNameTextBox;
                    return;
                }

                if (String.IsNullOrEmpty(PassWordTextBox.Text))
                {
                    ActiveControl = PassWordTextBox;
                    return;
                }

                Login_Click(sender, new EventArgs());
            }
        }
    }
}
